clear all
set more off
set maxvar 10000

global path "~/shared_space/dbaharc/BHOR-YugoslavianRefugees/"
global pathtmp "~/shared_space/cidgrowlab/bahartemp/"

/*************/
/*Atlas Data*/
/************/
clear 
g year=.
forvalues y = 1984/2014 {
	//append using "~/shared_space/dbaharc/General_Data/Atlas2013/sitc_trade_`y'.dta"
        append using "~/shared_space/dbaharc/General_Data/Atlas2015/Comtrade_`y'.dta"
	qui replace year=`y' if mi(year)
    }
keep year exporter importer commoditycode export_value import_value
append using "${path}data/SCGExports.dta" //Exports of SCG that were excluded from the Atlas data

//rename iso1 iso3_d //iso1 is the importer country
//rename iso2 iso3_o //iso2 is the exporter country
ren importer iso3_d
ren exporter iso3_o

//128 countries of Atlas + YUG, SCG and MNE (SCG is divided in SRB and MNE after 2005)
//ren iso3_d iso3
//merge m:1 iso3 using "${path}data/ISOInSampleAtlas.dta", keep(matched) nogen
//ren iso3 iso3_d
ren iso3_o iso3
merge m:1 iso3 using "${path}data/ISOInSampleAtlas.dta", keep(matched) nogen
ren iso3 iso3_o

keep if year>=1984 & year<=2014
ren export_value exp
ren import_value imp
ren iso3_d iso3_imp
ren iso3_o iso3_exp
ren commoditycode product

/*Converting all Former Yugoslavian republic to have the same code, YUG*/
preserve
keep if iso3_exp=="SCG" | iso3_exp=="SRB" | iso3_exp=="KOS" | iso3_exp=="HRV" ///
       | iso3_exp=="SVN" | iso3_exp=="MNE" | iso3_exp=="MKD" | iso3_exp=="BIH" ///
       | iso3_exp=="YUG"
ren iso3_exp iso3
collapse (sum) exp, by(iso3 year)
save "${path}data/ExportsYUGCountriesROW.dta", replace
restore

/*All YUG countries exports to ROW except DEU by product*/
/*
preserve
keep if iso3_exp=="SCG" | iso3_exp=="SRB" | iso3_exp=="KOS" | iso3_exp=="HRV" ///
       | iso3_exp=="SVN" | iso3_exp=="MNE" | iso3_exp=="MKD" | iso3_exp=="BIH" ///
       | iso3_exp=="YUG"
drop if iso3_imp=="DEU"
ren iso3_exp iso3
collapse (sum) exp_xdeu=exp, by(iso3 product year)
save "${path}data/ExportsYUGCountriesProductsROW.dta", replace
restore
*/


//Standardizing Serbia and Montenegro with one single iso3
replace iso3_exp = "SRB" if iso3_exp=="SCG"
replace iso3_imp = "SRB" if iso3_imp=="SCG"
replace iso3_exp = "SRB" if iso3_exp=="MNE"
replace iso3_imp = "SRB" if iso3_imp=="MNE"
//There are zero observations that drop here
drop if iso3_exp == "SRB" & iso3_imp =="SRB"

/*Loop used to exclude each one of the countris from total YUG exports*/
foreach c in SRB HRV SVN MKD BIH {
    preserve
    keep if iso3_exp=="`c'"
    drop if iso3_imp == "SRB" | iso3_imp == "KOS" | iso3_imp =="HRV" | iso3_imp == "SVN" | ///
        iso3_imp == "MNE" | iso3_imp == "MKD" | iso3_imp == "BIH"
    drop if iso3_imp =="DEU"
    collapse (sum) exp`c'_xdeu = exp, by(product year)
    g iso3_exp = "YUG"
    tempfile exports`c'
    save `exports`c''
    restore
}

/*Sum up all countries to become YUG*/
foreach c in SRB KOS HRV SVN MKD BIH {
    replace iso3_exp = "YUG" if iso3_exp=="`c'"
    replace iso3_imp = "YUG" if iso3_imp=="`c'"
}
/*Drop trade within former YUG countries*/
drop if iso3_exp=="YUG" & iso3_imp=="YUG"

/*Generating variables to exclude exports to Germany and save imports from Germany by product*/
g expdeu = exp if iso3_imp =="DEU"
g impdeu = imp if iso3_imp =="DEU"

rename iso3_imp wbcode
merge m:1 wbcode using "${path}data/World Bank Regions and Continents.dta", keep(master matched) nogen
replace eeurope=1 if wbcode=="ROU"
replace eeurope=1 if wbcode=="YUG"
rename wbcode iso3_imp
drop region
foreach v in easia_pac eeurope lac meast_nafr namerica sasia ssa weurope {
    ren `v' `v'_imp
    g exp`v' = exp  if `v'_imp
}

//Exports Data
collapse (sum) exp imp expdeu impdeu expeasia_pac expeeurope explac expmeast_nafr expnamerica expsasia expssa expweurope, by(iso3_exp product year)
g exp_xdeu = exp-expdeu
foreach v in easia_pac eeurope lac meast_nafr namerica sasia ssa weurope {
    g exp_x`v' = exp-exp`v'
}
rename iso3_exp iso3

sort iso3
egen countryid = group(iso3)

sort product
egen productid = group(product)

preserve
keep iso3 countryid
duplicates drop
save  "${path}data/CountriesID.dta",replace
restore

preserve
keep product productid
duplicates drop
save  "${path}data/ProductsID.dta", replace
restore

replace exp = 0 if mi(exp)
replace exp_xdeu = 0 if mi(exp_xdeu)
outsheet countryid productid year exp exp_xdeu imp impdeu ///
   expeasia_pac expeeurope explac expmeast_nafr expnamerica expsasia expssa expweurope ///
    using "${path}data/exports_raw.out", replace comma nonames

/*********************/
/* SENDING TO MATLAB */
/*********************/
shell matlab -nodesktop -r "run ${path}m_files/ComputeVars.m"


qui insheet using "${path}data/SimIndex.out", clear
#delimit ;
ren v1 countryid1; ren v2 countryid2; 
ren v3 simindex; ren v4 simindex_nolog; ren v5 simindex_fk; ren v6 year; 
#delimit cr

ren countryid1 countryid
merge m:1 countryid using "${path}data/CountriesID.dta", keep(matched master) nogen
drop countryid
ren iso3 iso3_o
ren countryid2 countryid
merge m:1 countryid using "${path}data/CountriesID.dta", keep(matched master) nogen
drop countryid
ren iso3 iso3_d
destring simindex simindex_nolog, replace force

keep if iso3_o=="YUG" | iso3_d=="YUG"
save "${path}data/YUGSimilarityIndex.dta", replace

qui insheet using "${path}/data/YugoslavianRefugees.out", clear
#delimit ;
ren v1 countryid; ren v2 productid; ren v3 year;
ren v4 rca; ren v5 exp; ren v6 expshare;
ren v7 rca_xdeu; ren v8 exp_xdeu; ren v9 expshare_xdeu; ren v10 imp; ren v11 impdeu;
ren v12 dens; ren v13 exptoeasiapac; ren v14 exptoeeurope; ren v15 exptolac;
ren v16 exptomeastnafr; ren v17 exptonamerica; ren v18 exptosasia; ren v19 exptossa;
ren v20 exptoweurope;
#delimit cr

/*
destring growth* lgrowth*, replace force
qui ds growth* lgrowth*, v(32)
local vlist = r(varlist)
foreach v of local vlist {
    replace `v' = . if exp==0
}
*/

/*Referencing with iso and SITC products:*/
merge m:1 countryid using "${path}data/CountriesID.dta", keep(matched master) nogen
merge m:1 productid using "${path}data/ProductsID.dta", keep(matched master) nogen

g FSU = (iso3=="RUS" | iso3=="GEO" | iso3=="UKR" | iso3 =="MDA" | iso3 == "BLR" | /// 
	iso3 =="ARM" | iso3 == "AZE" | iso3 == "KAZ" | iso3 == "UZB" | iso3 == "TKM" | ///
	iso3 =="KGZ" | iso3 == "TJK" | iso3 == "EST" | iso3 == "LVA" | iso3 == "LTU")

egen countryproductid = group(countryid productid)
egen countryyearid = group(countryid year)
egen productyearid = group(productid year)

/*Setting up as missing those obs for countries with zero exports in a given year*/
egen totalcountry = total(exp), by(countryid year)
foreach v in exp exp_xdeu rca rca_xdeu expshare expshare_xdeu {
    replace `v' = . if totalcountry==0
}
drop totalcountry

preserve
keep if iso3=="DEU"
keep product year rca exp
ren rca rcadeu
ren exp expdeu
tempfile deuexp
save `deuexp'
restore

/*Adding german exports only*/
merge m:1 product year using `deuexp', keep(master matched) nogen

foreach c in SRB HRV SVN MKD BIH {
    merge m:1 iso3 product year using `exports`c'', keep(master matched) nogen
    //g expno`c'_xdeu = exp_xdeu - exp`c'_xdeu
    //g expno`c'_xdeu = exp_xdeu if iso3=="YUG" & mi(exp`c'_xdeu)
    //replace expno`c'_xdeu = exp_xdeu - exp`c'_xdeu if iso3=="YUG" & !mi(exp`c'_xdeu)
}

//merge m:1 iso3 product year using `svnexports', keep(master matched) nogen

save "${path}data/YugoslavianRefugeesTradeDB.dta", replace 
erase "${path}/data/YugoslavianRefugees.out"


